package cc.flyflow.web.config;

import cc.flyflow.web.interceptor.CustomInterceptor;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * SaToken 权限配置
 *
 * @author Jyl
 */
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer
{

    @Resource
    private CustomInterceptor customInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry)
    {
        // 允许所有路径
        registry.addMapping("/**")
                // 允许所有来源
                .allowedOrigins("*")
                // 允许的 HTTP 方法
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                // 允许的请求头
                .allowedHeaders("*")
                // 允许发送 Cookie
                .allowCredentials(false)
                // 预检请求的缓存时间
                .maxAge(3600);
    }

    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry)
    {
        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
        registry.addInterceptor(customInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/login/*",
                        "/api/login/*",
                        "/remote/*",
                        "/test/*",
                        "/web/*",
                        "/css/*",
                        "/img/*",
                        "/fonts/*",
                        "/js/*",
                        "*.ico",
                        "/api/process-instance/notifyMsgEvent",
                        "/process-instance/notifyMsgEvent",
                        "/api/file/show/*",
                        "/api/file/show/*/*",
                        "/file/show/*/*",
                        "/file/show/*",
                        "/koTime",
                        "/error",
                        "/koTime/*");
    }
}
